package com.arm.service.impl;

import com.arm.dao.OrderItemMapper;
import com.arm.module.Order;
import com.arm.module.OrderItem;
import com.arm.module.ProductEntity;
import com.arm.module.User;
import com.arm.service.*;
import com.arm.tools.GenerateID;
import com.arm.tools.myRunTime.MyRunTimeExcption;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Baggio on 2017-11-22.
 */
@Service
public class OrderItemServiceImpl extends BaseService implements OrderItemService {

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    ProductEntityService productEntityService;
    @Autowired
    OrderService orderService;
    @Autowired
    UserService userService;

    @Override
    public Boolean modifyOrderItem(OrderItem orderItem) {
        orderItemMapper.updateByPrimaryKeySelective(orderItem);
        return true;
    }

    @Override
    public OrderItem getOrderItemByOrderItemId(Long orderItemId) {
        return orderItemMapper.getOrderItemByOrderItemId(orderItemId);
    }

    @Override
    public List<OrderItem> selectOrderItemList(Map<String, String> map) {
        return orderItemMapper.selectOrderItemList(map);
    }

    @Override
    public int updateOrderItemStatus(Map<String, String> map) {
        return orderItemMapper.updateOrderItemStatus(map);
    }

    @Override
    public OrderItem createOrderItem(Long productEntityId, Order order, Integer amount) {

        ProductEntity productEntity = productEntityService.getProductEntity(new HashMap<String, String>() {{
            put("productEntityId", String.valueOf(productEntityId));
        }});

        if (productEntity == null) {
            throw new MyRunTimeExcption("产品不存在！");
        }
        if (amount == null) {
            throw new MyRunTimeExcption("数量不存在！");
        }
        if (amount == 0) {
            throw new MyRunTimeExcption("数量错误！");
        }
        OrderItem orderItem = new OrderItem();
        orderItem.setOrderId(order.getOrderId());
        orderItem.setOrderItemId(GenerateID.CreatetID());
        orderItem.setProductEntityId(productEntityId);
        orderItem.setAmount(amount);
        orderItem.setPrice(productEntity.getPrice() * amount);
        orderItem.setCreateTime(new Date());
        orderItem.setStatus(OrderService.UNPAY);
        orderItemMapper.insertOrderItem(orderItem);
        return orderItem;
    }

    @Transactional
    @Override
    public Boolean cancelOrderItem(Long orderItemId) {

        OrderItem orderItem = orderItemMapper.selectOrderItemListByOrderItemId(orderItemId);
        Long orderId = orderItem.getOrderId();
        orderItem.setStatus(OrderService.CANCEL);
        if (orderItemMapper.updateOrderItemById(orderItem) != 1) {
            throw new MyRunTimeExcption("取消失败");
        }

        Order order = orderService.getOrderById(orderId);
        orderService.modifyOrder(order);
        return true;
    }

    @Override
    public Boolean modifyOrderItemStatus(Long orderItemId, Byte status) {

        OrderItem orderItem = orderItemMapper.selectOrderItemListByOrderItemId(orderItemId);
        orderItem.setStatus(status);
        orderItemMapper.updateOrderItemById(orderItem);

        return true;
    }

    @Override
    public Boolean completeOrderItem(Long orderItemId) {
        logger.info("completeOrderItem");
        OrderItem orderItem = orderItemMapper.selectOrderItemListByOrderItemId(orderItemId);

        if (orderItem == null) {
            throw new MyRunTimeExcption("订单项目没有找到！");
        }
        orderItem.setStatus(OrderService.COMPELTEED);


        orderItemMapper.updateOrderItemById(orderItem);

        checkOrderItem(orderItem.getOrderId());
        return true;
    }

    private void checkOrderItem(Long orderId) {
        Order order = orderService.getOrderById(orderId);
        List<OrderItem> orderItemList = order.getOrderItemList();
        for (OrderItem orderItems : orderItemList) {
            if (orderItems.getStatus() != OrderService.COMPELTEED && orderItems.getStatus() != OrderService.REFUNDED) {
                return;
            }
        }
        order.setStatus(OrderService.CLOSE);
        orderService.modifyOrder(order);
    }
}
